package com.chnbst.bstyun.controller.admin;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.GoodsFee;
import com.chnbst.bstyun.service.GoodsFeeService;
import com.chnbst.bstyun.service.GoodsService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author WangXinLei
 *
 */
@Controller
@RequestMapping("/sysAdmin")
public class FeeAdminController {
	@Autowired
	private GoodsFeeService goodsFeeService;
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 计费管理界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/feeAdmin/feeManager",method=RequestMethod.GET)
	public ModelAndView feeManager()throws Exception{
		
		List<GoodsFee> goodsFeeList = goodsFeeService.selectGrid();
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/admin/productManager/feeManager");
		mav.addObject("goodsFeeList", goodsFeeList);
		return mav;
	}
	
	/**
	 * @return 新增商品计费
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/feeAdmin/saveGoodsFee",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveGoodsFee(String id,String modename,String secvalue,Double inpval,String modelnote,Integer status,HttpServletRequest request)throws Exception{
		
		boolean isSuccess = false;
		
		GoodsFee goodsFee = new GoodsFee();
		goodsFee.setFeeFormula(secvalue);
		goodsFee.setFeeName(modename);
		goodsFee.setFeeBasenum(inpval);
		goodsFee.setGoodsFeeId(id);
		goodsFee.setRemark(modelnote);
		goodsFee.setIsDelete(0);
		goodsFee.setState(status);
		
		if(goodsFee.getGoodsFeeId() == null){
			isSuccess = goodsFeeService.insertGoodsFee(goodsFee);
		}else{
			isSuccess = goodsFeeService.updateGoodsFee(goodsFee);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		
		return map;
		
	}
	
	/**
	 * @return 查询单条商品计费
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/feeAdmin/selectGoodsFeeInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectGoodsFeeInfo(String id,HttpServletRequest request)throws Exception{
		boolean isOk = true;
		GoodsFee goodsFee = goodsFeeService.selectGoodsFeeVoInfo(id);
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(goodsFee == null){
			isOk = false;
		}
		map.put("isOk", isOk);
		map.put("goodsFee", goodsFee);
		
		return map;
		
	}
	
	/**
	 * @return 根据ID 删除商品计费
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/feeAdmin/deleteByFeeId",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteByFeeId(String ids,HttpServletRequest request)throws Exception{
		
		boolean isDelete = true;
		
		String[] goodsFeeList = ids.split(",");
		for(int i=0;i<goodsFeeList.length;i++){
			List<Goods> goodsList = goodsService.selectGoodsByFeeId(goodsFeeList[i]);
			if(goodsList.size() > 0){
				isDelete = false;
			}else{
				GoodsFee goodsFee = goodsFeeService.selectGoodsFeeVoInfo(goodsFeeList[i]);
				goodsFee.setIsDelete(1);
				goodsFeeService.updateGoodsFee(goodsFee);
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isDelete", isDelete);
		
		return map;
		
	}
	
	/**
	 * @return 根据ID 停用商品计费
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/feeAdmin/stopByTypeId",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> stopByTypeId(String id,Integer state,HttpServletRequest request)throws Exception{
		
		boolean isStop = true;
		
		List<Goods> goodsList = goodsService.selectGoodsByFeeId(id);
		if(goodsList.size() > 0){
			isStop = false;
		}else{
			GoodsFee goodsFee = goodsFeeService.selectGoodsFeeVoInfo(id);
			goodsFee.setState(state);
			isStop = goodsFeeService.updateGoodsFee(goodsFee);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isStop", isStop);
		
		return map;
	}	
}
